package model

import (
	"github.com/go-xorm/xorm"
)

// BorwRule represents the t_borwrule table.
type BorwRule struct {
	ID       int64  `xorm:"'id' pk autoincr"`
	FlowTP   string `xorm:"'flowtp'"`
	MaxJS    int    `xorm:"'maxjs'"`
	MaxJQ    int    `xorm:"'maxjq'"`
	MaxXQ    int    `xorm:"'maxxq'"`
	FMNY     string `xorm:"'fmny'"`
	MindFBS  int    `xorm:"'mindfbs'"`
	ReaderID string `xorm:"'readerid'"`
}

// TableName returns the name of the table in the database.
func (b BorwRule) TableName() string {
	return "t_borwrule"
}

// GetBorwRule retrieves a record by ID from the database.
func GetBorwRule(engine *xorm.Engine, id int) (*BorwRule, error) {
	record := new(BorwRule)
	has, err := engine.ID(id).Get(record)
	if err != nil {
		return nil, err
	}
	if !has {
		return nil, xorm.ErrNotExist
	}
	return record, nil
}

func UpdateBorwRule(engine *xorm.Engine, newRecord *BorwRule) error {
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	_, err := session.ID(newRecord.ID).Update(newRecord)
	if err != nil {
		if rErr := session.Rollback(); rErr != nil {
			return rErr
		}
		return err
	}
	if err := session.Commit(); err != nil {
		return err
	}
	return nil
}
